import { createRouter, createWebHistory } from "vue-router";
import routes from "./routes";
import { usePageLoad } from "@/stores/usePageLoad";

const router = createRouter({
	history: createWebHistory(import.meta.env.BASE_URL),
	routes,
	// 激活时的滚动行为
	scrollBehavior(_to, _from, savedPosition) {
		return savedPosition ? savedPosition : { top: 0 };
	},
});
// 配置跳转前要做什么
router.beforeEach((_to, _from, next) => {
	const pageLoad = usePageLoad();
	pageLoad.setState({ show: true });
	// if (to.meta.needLogin && !userStore.token) next("/login");
	next();
});
// 配置跳转后要做什么
router.afterEach((to, _from) => {
	document.title = to.name as string;
	const pageLoad = usePageLoad();
	pageLoad.setState({ show: false });
});

export default router;
